﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;
using System.Web.UI.WebControls;
using System.Web;

namespace Portal.CMS
{
    public class ReDocumentUpload : WebPart
    {

        public override string WebPartId
        {
            get
            {
                return "ReDocumentUpload";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Upload lại tài liệu");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }


        System.Web.UI.WebControls.Button SaveButtonLargeOnly = null;
        System.Web.UI.WebControls.Label labelError = null;
        System.Web.UI.WebControls.TextBox txtDocumentItemName = null;
        System.Web.UI.WebControls.TextBox txtDescription = null;
        System.Web.UI.WebControls.TextBox txtUrl = null;

        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl, System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            try
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(ReDocumentUpload));
                string DocumentItemId = (string)System.Web.HttpContext.Current.Request["DocumentItemId"];
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "DocumentItems");
                string owner = DocumentItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ReUploadDocumentItem", DocumentItemId, owner, true);


                labelError = new System.Web.UI.WebControls.Label();
                labelError.CssClass = "ErrorUploadNewsImage";

                SaveButtonLargeOnly = new Button();
                SaveButtonLargeOnly.Text = ResourceBLL.GetText(WebPartId, "Upload", "Upload tài liệu");
                SaveButtonLargeOnly.Click += new EventHandler(SaveButtonLargeOnly_Click);


                txtDescription = new TextBox();
                txtDescription.TextMode = System.Web.UI.WebControls.TextBoxMode.MultiLine;

                txtUrl = new TextBox();
                txtUrl.CssClass = "cssUploadAdvImageItem";

                txtDocumentItemName = new TextBox();
                txtDocumentItemName.CssClass = "cssUploadAdvImageItem";


                pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetHeaderFormDialog(ResourceBLL.GetText(WebPartId, "Title", "Upload lại tài liệu!"))));
                pl.Controls.Add(labelError);
                pl.Controls.Add(new System.Web.UI.LiteralControl("<table>"));
                pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
                pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
                pl.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText(WebPartId, "FileName", "Select file:")));
                pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
                pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
                pl.Controls.Add(new System.Web.UI.LiteralControl("<input name=\"txtItemName\" id=\"txtItemName\" type=\"file\" runat=server>"));
                pl.Controls.Add(SaveButtonLargeOnly);
                pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
                pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));



                pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
                pl.Controls.Add(new System.Web.UI.LiteralControl("<td></td>"));
                pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));

                pl.Controls.Add(new System.Web.UI.LiteralControl("<input type=button onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "CloseButton", "Thoát") + "\">"));
                pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
                pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));
                pl.Controls.Add(new System.Web.UI.LiteralControl("</table>"));
                pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetFooterFormDialog()));
            }
            catch (Exception ex)
            {
                pl.Controls.Clear();
                pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetHeaderFormDialog(ResourceBLL.GetText(WebPartId, "Title", "Upload lại tài liệu!"))));
                pl.Controls.Add(new System.Web.UI.LiteralControl(ex.Message.ToString()));
                pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetFooterFormDialog()));
            }
        }

        void SaveButtonLargeOnly_Click(object sender, EventArgs e)
        {
            try
            {
                string DocumentItemId = (string)System.Web.HttpContext.Current.Request["DocumentItemId"];
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                labelError.Text = "";
                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

                HttpPostedFile hf = (HttpPostedFile)files[0];

                string PathSave = Environments.WorkingPath + "\\media\\" + Environments.PortalAlias;
                System.IO.Directory.CreateDirectory(PathSave);

                PathSave = Environments.WorkingPath + "\\media\\" + Environments.PortalAlias + "\\Document";
                System.IO.Directory.CreateDirectory(PathSave);

                PathSave = Environments.WorkingPath + "\\media\\" + Environments.PortalAlias + "\\Document\\" + DocumentItemCls.belongSite;
                System.IO.Directory.CreateDirectory(PathSave);

                PathSave = Environments.WorkingPath + "\\media\\" + Environments.PortalAlias + "\\Document\\" + DocumentItemCls.belongSite + "\\Archives";
                System.IO.Directory.CreateDirectory(PathSave);

                string OldFile = DocumentItemCls.FileUpload;
                string FileSave = PathSave + "\\" + OldFile;
                if (hf.FileName.Length > 0)
                {
                    try
                    {
                        System.IO.File.Delete(FileSave);
                    }
                    catch { }
                    string NewFile = new System.IO.FileInfo(hf.FileName).Name;
                    FileSave = PathSave + "\\" + NewFile;
                    hf.SaveAs(FileSave);
                    byte[] b = Environments.GetBytesFromImageFile(FileSave);
                    new DocumentBLL().UpdateDocumentItemContent(null, DocumentItemId, b, NewFile);
                    try
                    {
                        System.IO.File.Delete(FileSave);
                    }
                    catch
                    {
                    }
                }

                labelError.Text = ResourceBLL.GetText(WebPartId, "UploadOk", "Cập nhật tài liệu thành công!");
            }
            catch (Exception ex)
            {
                labelError.Text = ex.Message.ToString();
            }
        }
    }
}